package TDALista;


import java.util.*;

public class ElementIterator<E> implements Iterator <E> {
	
    protected PositionList<E> list;
    protected Position<E> cursor;
    
    public ElementIterator(PositionList <E> l) {
    	list = l;
    	if(list.isEmpty())
    		cursor = null;
    	else cursor = list.first();
    }
    
    public boolean hasNext() {
    	return cursor != null;
    }
    
    public E next () throws NoSuchElementException {
        if(cursor == null)
           throw new NoSuchElementException ("No tiene siguiente");
        E toReturn = cursor.element();
        try {
        	cursor = (cursor == list.last()) ? null : list.next(cursor);
        }catch(BoundaryViolationException e) {
        	System.out.println("Error "+e.getMessage());
        }catch(InvalidPositionException e) {
        	System.out.println("Error "+e.getMessage());
        }
        return toReturn;
    }
    
    public void remove() {}
}
